/**
 * 
 */
package complex.cs;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zhenghx
 * C/S结构聊天室 - 服务器端程序
 * 服务器端程序实现多个用户请求的功能
 */
public class Server {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ServerService MyServer = new ServerService(6544, 10); // 建立服务器对象
	}
}

class ServiceThread extends Frame implements Runnable {

	// 当CLIENT有请求时，SERVER端创建一个FRAME用于与之交互数据
	ServerService FatherListener;

	Socket ConnectedClient;

	Thread ConnectThread;

	Panel ListenerPanel;

	TextArea ServerMeg;

	public ServiceThread(ServerService sv, Socket s) { // 构造函数
		FatherListener = sv;
		ConnectedClient = s;
		ConnectThread = new Thread(this);
		setTitle("Server(服务器端)");
		setLayout(new BorderLayout());
		ServerMeg = new TextArea(13, 50);
		add("Center", ServerMeg);
		setResizable(false);
		pack();
		show();
		InetAddress ClientAddress = ConnectedClient.getInetAddress();
		// 获取请求服务的CLIENT端计算机的IP地址
		ServerMeg.appendText("Server connect to:" + "\n\n"
				+ ClientAddress.toString() + ".\n");
	}

	public void run() {
		try {
			// 获取从CLIENT读入的数据流
			DataInputStream in = new DataInputStream(new BufferedInputStream(
					ConnectedClient.getInputStream()));
			// 获取向CLIENT输出的数据流
			//PrintStream out = new PrintStream(new BufferedOutputStream(
			//		ConnectedClient.getOutputStream()));
			//out.println("Hello! Welcome connect to me(Server)!\r");
			//out.flush(); // 向CLIENT端输出信息
			String s = in.readLine();
			// 当客户端发出“Bye”消息，服务器中断与它的Socket连接
			while (!s.equals("Bye")) {
				ServerMeg.appendText("Client>>" + s + "\n");
				s = in.readLine();				
			}
			ConnectedClient.close(); // 读入CLIENT端写入的下一行信息
		} catch (Exception e) {
			e.printStackTrace();
		}
		FatherListener.addMeg("Client closed. \n");
		dispose();
	}

}

/**
 * @author zhenghx
 * 服务器端的监听窗口
 */
class ServerService extends Frame {

	ServerSocket m_sListener; // 监听器

	TextArea ListenerMeg; // 显示信息的监听器窗口

	public ServerService(int Port, int Count) {
		try { // 建立监听服务
			m_sListener = new ServerSocket(Port, Count);
			setTitle("Server Listener(监听器窗口)");
			this.addWindowFocusListener(new WinAdpt()); // 建立监听服务的窗口并显示
			setLayout(new BorderLayout());
			ListenerMeg = new TextArea("[监听服务已启动]\n\n\n", 10, 50);
			add("Center", ListenerMeg);
			setResizable(false);
			pack();
			show();
			while (true) {
				Socket Connected = m_sListener.accept(); // 接受来自Client段的请求
				InetAddress ClientAddress = Connected.getInetAddress(); // 得到IP地址
				ListenerMeg.appendText("Client connected from:\n\n"
						+ ClientAddress.toString() + ".\n");
				// 获取客户机的IP地址，建立新线程新窗口与这个CLIENT通信
				ServiceThread MyST = new ServiceThread(this, Connected);
				MyST.ConnectThread.start(); // 线程开始运行
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addMeg(String s) {
		ListenerMeg.appendText(s); // 在监听器上添加信息
	}

}

class WinAdpt extends WindowAdapter {
	public void windowClosing(WindowEvent e) {
		((Frame) e.getWindow()).dispose();
		System.exit(0); // 关闭窗体
	}
}
